package cx.mybatis.session.defaults;

import cx.mybatis.mapping.MappedStatement;
import cx.mybatis.session.Configuration;
import cx.mybatis.session.SqlSession;

/**
 * 默认的 SqlSession实现
 * @Author:cxy
 * @Date:2022/4/9 10:55
 */
public class DefaultSqlSession implements SqlSession {

    private final Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <T> T selectOne(String statement) {
        return null;
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T> T selectOne(String statement, Object parameter) {
        MappedStatement mappedStatement = configuration.getMappedStatement(statement);
        return (T) ("你被代理了！" + "\n方法：" + statement + "\n入参：" + parameter.toString() + "\n待执行SQL：" + mappedStatement.getSql());
    }

    /**
     * 通过mapperRegistry 获取Mapper代理类
     * @param type Mapper interface class 类型
     * @param <T> 泛型约束
     * @return 代理类Mapper
     */
    @Override
    public <T> T getMapper(Class<T> type) {
        return configuration.getMapper(type,this);
    }
}
